package cn.etynit.controller;

import cn.etynit.block.GetUserByIdFallback;
import cn.etynit.domain.User;
import cn.etynit.feign.UserService;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/order")
@RefreshScope // 动态刷新配置注解
public class OrderController {
    @Autowired
    private UserService userService;

    /**
     * value: 当前接口的资源名称，也就是别名
     * blockHandler：指定触发限流之后的托底数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @SentinelResource(value = "getUserById",
            blockHandler = "getUserByIdBlockHandler",
            blockHandlerClass = GetUserByIdFallback.class,
            fallback = "getUserByIdFallback",
            fallbackClass = GetUserByIdFallback.class)
    public User getUserById(@PathVariable("id") Long id){
        // 用于让服务慢响应
        return userService.getUserById(id);
    }


//    public User getUserByIdBlockHandler(@PathVariable("id") Long id, BlockException e){
//        e.printStackTrace();
//        return new User(-1L,"王大炮","断了。。。。");
//    }

//    public User getUserByIdFallback(@PathVariable("id") Long id,Throwable throwable){
//        return new User(-1L,"熔断","我断了。。。。");
//    }
}